package com.fenghongzhang.day002;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class PopupWindowActivity extends AppCompatActivity {
    private ImageView add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //只出当前页面的title
        //supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_popup_window);


        add = (ImageView) findViewById(R.id.add);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //popupwindow
                final PopupWindow popupWindow = new PopupWindow(PopupWindowActivity.this);

                View inflate = LayoutInflater.from(PopupWindowActivity.this).inflate(R.layout.popup_window_layout, null);
                TextView viewById = inflate.findViewById(R.id.chat);
                viewById.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(PopupWindowActivity.this, "呵呵", Toast.LENGTH_SHORT).show();
                        popupWindow.dismiss();
                    }
                });
                //布局文件
                popupWindow.setContentView(inflate);
                //指定宽高
                popupWindow.setHeight(300);
                popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);

                //点击外部可消失
                popupWindow.setOutsideTouchable(true);


                //加一个遮罩, 修改window的不透明度
                WindowManager.LayoutParams attributes = getWindow().getAttributes();
                attributes.alpha = 0.5f;
                getWindow().setAttributes(attributes);

                //当pop 消失的时候, 在还原透明度
                popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        WindowManager.LayoutParams attributes = getWindow().getAttributes();
                        attributes.alpha = 1f;
                        getWindow().setAttributes(attributes);
                    }
                });


                //添加一个动画
                popupWindow.setAnimationStyle(R.style.pop); //自己写一个动画文件

                //在组件下方
//                popupWindow.showAsDropDown(add);

                View main = LayoutInflater.from(PopupWindowActivity.this).inflate(R.layout.activity_popup_window, null);
                //在指定位置
                popupWindow.showAtLocation(main, Gravity.CENTER,0,300);

            }
        });

    }
}
